import java.util.Arrays;

public class Solution1624 {
    public int maxLengthBetweenEqualCharacters(String s) {
        int[] start=new int[26];
        int[] end=new int[26];
        Arrays.fill(start,-1);
        Arrays.fill(end,-1);
        for (int i=0;i<s.length();i++) {
            char c=s.charAt(i);
            if (start[c-'a']==-1){
                start[c-'a']=i;
            }
            end[c-'a']=i;
        }
        int ans=-1;
        for (int i = 0; i < 26; i++) {
            if (start[i]!=end[i]){
                ans=Math.max(ans,end[i]-start[i]-1);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1624().maxLengthBetweenEqualCharacters("abca"));
    }
}
